﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net.Mail;
using System.Net;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using Darius.API.Application.Commands;
using MediatR;
using IMES.Common.Domain.DariusModel;

namespace Darius.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EmailController : ControllerBase
    {
        private readonly IMediator Mediator;

        public EmailController(IMediator mediator)
        {
            Mediator = mediator;
        }
        //六位验证码
        static string cood = createrandom(6).ToLower();
        string cood2 = cood;
        private static string createrandom(int codelengh)
        {
            int rep = 0;
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + rep;
            rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
            for (int i = 0; i < codelengh; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            return str;
        }


        /// <summary>
        /// 发送验证码
        /// </summary>
        [HttpPost]
        public ActionResult Code([FromQuery] string email_r)
        {
            if (email_r.Contains("@"))
            {
                //实例化一个发送邮件类。
                MailMessage mailMessage = new MailMessage();
                //发件人邮箱地址，方法重载不同，可以根据需求自行选择。
                mailMessage.From = new MailAddress("45134833@qq.com");
                //收件人邮箱地址。
                mailMessage.To.Add(new MailAddress(email_r));
                //邮件标题。
                mailMessage.Subject = "这是你的验证码为：";
                //邮件内容。
                mailMessage.Body = "验证码：" + cood + "。此验证码只用于登录IMES验证身份，请勿转发他人。";
                //实例化一个SmtpClient类。
                SmtpClient client = new SmtpClient();
                //在这里我使用的是qq邮箱，所以是smtp.qq.com，如果你使用的是126邮箱，那么就是smtp.126.com。
                client.Host = "smtp.qq.com";
                client.Port = 587;
                //使用安全加密连接。
                client.EnableSsl = true;
                //不和请求一块发送。
                client.UseDefaultCredentials = false;
                //验证发件人身份(发件人的邮箱，邮箱里的生成授权码);
                client.Credentials = new NetworkCredential("45134833@qq.com", "ohdyxmmeuphnbijc");
                //发送
                client.Send(mailMessage);
                return Content("发送成功");
            }
            return Content("邮箱格式有误，请重新输入。");
        }
        /// <summary>
        /// 添加邮箱验证码
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> AddEmail(EmailCreateCommand cmd)
        {
            cmd.Code = cood2;
            return await Mediator.Send(cmd);
        }
        /// <summary>
        /// 邮箱登录
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> EmailLogin(EmailLoginSearchCommand cmd)
        {
            return await Mediator.Send(cmd);
        }


    }
}
